package cn.com.homeyoda.aimate.aihealth.ui.home;

import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.base.BaseViewModel;
import cn.com.homeyoda.aimate.aihealth.bean.ArticleBean;
import cn.com.homeyoda.aimate.aihealth.bean.BannerBean;
import cn.com.homeyoda.aimate.aihealth.bean.BaseArticleBean;
import cn.com.homeyoda.aimate.aihealth.bean.BaseResponse;
import cn.com.homeyoda.aimate.aihealth.bean.KePuBean;
import cn.com.homeyoda.aimate.aihealth.bean.ReMenBean;
import cn.com.homeyoda.aimate.aihealth.bean.ZuiXinBean;
import cn.com.homeyoda.aimate.aihealth.data.Repository;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.wzq.mvvmsmart.event.SingleLiveEvent;
import com.wzq.mvvmsmart.event.StateLiveData;
import com.wzq.mvvmsmart.http.ResponseThrowable;
import com.wzq.mvvmsmart.utils.KLog;
import com.wzq.mvvmsmart.utils.RxUtils;
import com.wzq.mvvmsmart.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created By 刘纯贵
 * Created Time 2020/4/30
 */
public class HomeViewModel extends BaseViewModel<Repository> {

    public int pageNum = 0;
    public StateLiveData<List<ArticleBean>> stateLiveData;
    public StateLiveData<List<BannerBean>> stateBannerLiveData;
    public StateLiveData<Object> stateLiveCollData;
    public StateLiveData<Object> stateLiveUnCollData;

    public StateLiveData<List<ZuiXinBean>> zuixinData;
    public StateLiveData<List<ReMenBean>> reMenData;
    public StateLiveData<List<KePuBean>> kePuData;

    public HomeViewModel(@NonNull Application application, Repository model) {
        super(application, model);
        stateLiveData = new StateLiveData<>();
        stateLiveData.setValue(new ArrayList<ArticleBean>());

        stateBannerLiveData = new StateLiveData<>();
        stateBannerLiveData.setValue(new ArrayList<>());

        stateLiveCollData = new StateLiveData<>();
        stateLiveCollData.setValue(new Object());

        stateLiveUnCollData = new StateLiveData<>();
        stateLiveUnCollData.setValue(new Object());

        zuixinData = new StateLiveData<>();
        zuixinData.setValue(new ArrayList<ZuiXinBean>());

        reMenData = new StateLiveData<>();
        reMenData.setValue(new ArrayList<ReMenBean>());

        kePuData = new StateLiveData<>();
        kePuData.setValue(new ArrayList<KePuBean>());
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //下拉刷新完成
        public SingleLiveEvent<Object> finishRefreshing = new SingleLiveEvent<Object>();
        //上拉加载完成
        public SingleLiveEvent<Object> finishLoadMore = new SingleLiveEvent<Object>();
    }

    public void zuixinData(){
        List<ZuiXinBean> zuiXinBeanList = new ArrayList<>();
        ZuiXinBean zuiXinBean = new ZuiXinBean();
        zuiXinBean.setTitle("通知");
        zuiXinBean.setContent("紧急通知");
        zuiXinBean.setSelect(true);
        zuiXinBean.setTime("2025-02-26 11:36:53");
        ZuiXinBean zuiXinBean2 = new ZuiXinBean();
        zuiXinBean2.setTitle("用药提醒");
        zuiXinBean2.setContent("该吃心脏病的药了");
        zuiXinBean2.setSelect(false);
        zuiXinBean2.setTime("2025-02-26 11:25:42");
        ZuiXinBean zuiXinBean3 = new ZuiXinBean();
        zuiXinBean3.setTitle("用药提醒");
        zuiXinBean3.setContent("改吃降血压的药了");
        zuiXinBean3.setSelect(false);
        zuiXinBean3.setTime("2025-02-26 11:11:25");
        zuiXinBeanList.add(zuiXinBean);
        zuiXinBeanList.add(zuiXinBean2);
        zuiXinBeanList.add(zuiXinBean3);
        zuixinData.postSuccess();
        zuixinData.getValue().addAll(zuiXinBeanList);
        zuixinData.postValueAndSuccess(zuixinData.getValue());
    }
    public void remenData(){
        List<ReMenBean> reMenBeans = new ArrayList<>();
        ReMenBean reMenBean = new ReMenBean();
        reMenBean.setTitle("发不的活动一");
        reMenBean.setContent("去春游，五四广场集合");
        reMenBean.setTime("2025-03-8 11:36:53");
        reMenBean.setRenshu("80");
        reMenBean.setBaomingNum("38");
        ReMenBean reMenBean2 = new ReMenBean();
        reMenBean2.setTitle("发不的活动二");
        reMenBean2.setContent("去爬山，崂山集合");
        reMenBean2.setTime("2025-03-9 09:30:53");
        reMenBean2.setRenshu("80");
        reMenBean2.setBaomingNum("38");

        reMenBeans.add(reMenBean);
        reMenBeans.add(reMenBean2);
        reMenData.postSuccess();
        reMenData.getValue().addAll(reMenBeans);
        reMenData.postValueAndSuccess(reMenData.getValue());
    }

    public void kepuData(){
        List<KePuBean> kePuBeans = new ArrayList<>();
        KePuBean kePuBean = new KePuBean();
        kePuBean.setContent("吃十颗抵一颗？这才是维生素最高的水果");
        kePuBean.setTime("2025-03-9 10:36:53");
        kePuBean.setName("李四");
        kePuBean.setNum("11.5万");
        Bitmap bitmap = BitmapFactory.decodeResource(getApplication().getResources(),R.mipmap.one);
        kePuBean.setImgPath(bitmap);

        KePuBean kePuBean1 = new KePuBean();
        kePuBean1.setContent("吃八颗抵一颗？这才是维生素最高的水果");
        kePuBean1.setTime("2025-03-10 10:36:53");
        kePuBean1.setName("王五");
        kePuBean1.setNum("10.3万");
        Bitmap bitmap1 = BitmapFactory.decodeResource(getApplication().getResources(),R.mipmap.two);
        kePuBean1.setImgPath(bitmap1);

        KePuBean kePuBean2 = new KePuBean();
        kePuBean2.setContent("吃五颗抵一颗？这才是维生素最高的水果");
        kePuBean2.setTime("2025-03-11 10:36:53");
        kePuBean2.setName("马六");
        kePuBean2.setNum("9.8万");
        Bitmap bitmap2 = BitmapFactory.decodeResource(getApplication().getResources(),R.mipmap.three);
        kePuBean2.setImgPath(bitmap2);

        KePuBean kePuBean3 = new KePuBean();
        kePuBean3.setContent("吃三颗抵一颗？这才是维生素最高的水果");
        kePuBean3.setTime("2025-03-12 10:36:53");
        kePuBean3.setName("张三");
        kePuBean3.setNum("6.7万");
        Bitmap bitmap3 = BitmapFactory.decodeResource(getApplication().getResources(),R.mipmap.four);
        kePuBean3.setImgPath(bitmap3);

        kePuBeans.add(kePuBean);
        kePuBeans.add(kePuBean1);
        kePuBeans.add(kePuBean2);
        kePuBeans.add(kePuBean3);
        kePuData.postSuccess();
        kePuData.getValue().addAll(kePuBeans);
        kePuData.postValueAndSuccess(kePuData.getValue());
    }

    /**
     * 获取首页文章数据列表
     */
    public void requestData() {
        model.getArticle(pageNum)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(HomeViewModel.this)
                .subscribe(new Observer<BaseResponse<BaseArticleBean<List<ArticleBean>>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        stateLiveData.postLoading();
                    }

                    @Override
                    public void onNext(BaseResponse<BaseArticleBean<List<ArticleBean>>> baseArticleBeanBaseResponse) {
                        KLog.e("进入onNext");
                        //请求成功
                        if (baseArticleBeanBaseResponse.getErrorCode() == 0) {
                            List<ArticleBean> data = baseArticleBeanBaseResponse.getData().getDatas();
                            if (data != null){
                                if (data.size() > 0){
                                    if (pageNum == 0) {
                                        stateLiveData.getValue().clear();
                                    }
                                    stateLiveData.postSuccess();
                                    stateLiveData.getValue().addAll(data);
                                    stateLiveData.postValueAndSuccess(stateLiveData.getValue());
                                }else {
                                    ToastUtils.showShort("没有更多数据了");
                                    KLog.e("请求到数据students.size" + data.size());
                                }
                            }else {
                                KLog.e("数据返回null");
                                stateLiveData.postError();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        //关闭对话框
                        stateLiveData.postError();
                        if (e instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) e).message);
                        }
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                        //请求刷新完成收回
                        stateLiveData.postIdle();
                        uc.finishRefreshing.call();
                        uc.finishLoadMore.call();
                    }
                });
    }

    //获取首页轮播图
    public void requestBannerData() {
        List<BannerBean> bannerBeanList = new ArrayList<>();
        BannerBean bannerBean = new BannerBean();
        bannerBean.setId(R.mipmap.banner1);
        BannerBean bannerBean2 = new BannerBean();
        bannerBean2.setId(R.mipmap.banner2);
        bannerBeanList.add(bannerBean);
        bannerBeanList.add(bannerBean2);
        stateBannerLiveData.postSuccess();
        stateBannerLiveData.getValue().addAll(bannerBeanList);
        stateBannerLiveData.postValueAndSuccess(stateBannerLiveData.getValue());
        Log.i("haha","bannerBeanList.size====="+bannerBeanList.size());
//        model.getBanner()
//                .compose(RxUtils.observableToMain())
//                .compose(RxUtils.exceptionTransformer())
//                .doOnSubscribe(HomeViewModel.this)
//                .subscribe(new Observer<BaseResponse<List<BannerBean>>>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//                        stateBannerLiveData.postLoading();
//                    }
//
//                    @Override
//                    public void onNext(BaseResponse<List<BannerBean>> listBaseResponse) {
//                        if (listBaseResponse.getErrorCode() == 0){
//                            List<BannerBean> datas = listBaseResponse.getData();
//                            if (datas != null){
//                                if (datas.size() > 0){
//                                    stateBannerLiveData.getValue().addAll(datas);
//                                    stateBannerLiveData.postValueAndSuccess(stateBannerLiveData.getValue());
//                                }else {
//                                    ToastUtils.showShort("没有更多数据了");
//                                    KLog.e("请求到数据students.size" + datas.size());
//                                }
//                            }else {
//                                KLog.e("数据返回null");
//                                stateBannerLiveData.postError();
//                            }
//                        }
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        KLog.e("进入onError" + e.getMessage());
//                        stateBannerLiveData.postIdle();
//                        if (e instanceof ResponseThrowable) {
//                            ToastUtils.showShort(((ResponseThrowable) e).message);
//                        }
//                    }
//
//                    @Override
//                    public void onComplete() {
//                        KLog.e("进入onComplete");
//                        stateBannerLiveData.postIdle();
//                    }
//                });
    }

    //收藏文章
    public void collectionArticle(int id){
        model.collectionArticle(id)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new Observer<BaseResponse>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        stateLiveCollData.postLoading();
                    }

                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (baseResponse.getErrorCode() == 0){
                            stateLiveCollData.postSuccess();
                            ToastUtils.showShort("收藏成功！");
                        }else {
                            stateLiveCollData.postError();
                            ToastUtils.showShort("收藏失败！"+baseResponse.getErrorMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        stateLiveCollData.postError();
                        ToastUtils.showShort("收藏失败！"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    // 取消收藏文章
    public void unCollectionArticle(int id){
        model.unCollectionArticle(id)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new Observer<BaseResponse>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        stateLiveUnCollData.postLoading();
                    }

                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (baseResponse.getErrorCode() == 0){
                            stateLiveUnCollData.postSuccess();
                            ToastUtils.showShort("取消收藏成功！");
                        }else {
                            stateLiveUnCollData.postError();
                            ToastUtils.showShort("取消收藏失败！"+baseResponse.getErrorMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        stateLiveUnCollData.postError();
                        ToastUtils.showShort("取消收藏失败！"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });

    }
}
